//
//  GGChatViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/4/2.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGChatViewController.h"
#import "GGAddtion.h"
#import "ZFBChatModel.h"
#import "GGChatCell.h"
#import "GGOtherChatCell.h"
static NSString *cellID = @"cellID";
static NSString *otherID = @"otherID";
@interface GGChatViewController ()<UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray <ZFBChatModel *>*chatList;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation GGChatViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"聊天";
    [self loadData];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[GGChatCell class] forCellReuseIdentifier:cellID];
    [self.tableView registerClass:[GGOtherChatCell class] forCellReuseIdentifier:otherID];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.chatList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZFBChatModel *model = self.chatList[indexPath.row];
    if(model.type == 0){
        GGChatCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    GGOtherChatCell *cell = [tableView dequeueReusableCellWithIdentifier:otherID forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)loadData{
    self.chatList = [NSArray gg_arrayModelWithPlistName:@"chats.plist" withClassName:@"ZFBChatModel"].mutableCopy;
    
    //去除时间相同显示
    NSString *timeNil = nil;
    //第一个时间肯定是存在的
    for (ZFBChatModel *model in self.chatList) {
        if([timeNil isEqualToString:model.time])
            model.time = nil;
        else
            timeNil = model.time;
    }
}
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 200;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellAccessoryNone;
    }
    return _tableView;
}
- (NSMutableArray <ZFBChatModel *>*)chatList{
    if(!_chatList){
        _chatList = [NSMutableArray array];
    }
    return _chatList;
}
@end
